from pygame import display as pdis
from pygame.constants import MOUSEBUTTONDOWN

from lib.ZNode import ZNode
from lib.res.fun import _create_font
from lib.res.glo import COLOR , default_font


class ZLabel ( ZNode ) :
    def __init__ ( self ,
                   father ,
                   text = '' ,
                   pos = [ 0 , 0 ] ,
                   color = COLOR [ 'font' ] [ 'color' ] [ 1 ] ,
                   shadow = COLOR [ 'font' ] [ 'shadow' ] [ 0 ] ,
                   font = default_font ) :
        super ( ZLabel , self ).__init__ ( father )
        self.color = color
        self.font = font
        self.shadow = shadow
        self.text = text
        self.color = color
        self.shadow = shadow
        self.image = _create_font ( self.text , color , font )
        self.image_shadow = _create_font ( self.text , shadow , font )
        self._pos = pos
        self.screen = pdis.get_surface ( )
        self._running = True

    def draw ( self , **kwargs) :
        if self._running :
            if kwargs :
                self.image = self.image_shadow = None
                self.image = _create_font ( kwargs.get ( 'text' ) , self.color , self.font )
                self.image_shadow = _create_font ( kwargs.get ( 'text' ) , self.shadow , self.font )
                self.screen.blit ( self.image_shadow ,
                                   map ( lambda (a , b ) : a + b , zip ( self._pos , [ 1 , 1 ] ) ) )
                self.screen.blit ( self.image , self._pos )

    def update ( self , event , pos , key , **kwargs ) :
        super ( ZLabel , self ).update ( event , pos , key )
        if kwargs and event.type == MOUSEBUTTONDOWN :
            self.image = self.image_shadow = None
            self.image = _create_font ( kwargs.get ( 'text' ) , self.color , self.font )
            self.image_shadow = _create_font ( kwargs.get ( 'text' ) , self.shadow , self.font )
